Remove the arguments from `BuildConfig` and append them to the profile that's being...
authorSondre Lefsaker <sondrele@stud.ntnu.no>
Sat, 2 May 2015 14:30:16 +0000 (16:30 +0200)
committerSondre Lefsaker <sondrele@stud.ntnu.no>
Sat, 2 May 2015 15:43:11 +0000 (17:43 +0200)
- An error will be returned if the length of `targets` is not 1
- The profile of `targets` gets cloned in order to append the extra arguments.
- The new test verifies that the build fails due to both `lib` and `main` being compiled

src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_rustc/mod.rs
tests/test_cargo_rustc.rs

index 38a71dc76d98979d6e1580b22bd27b78c808067a..0b7988c3975195da7411c713346b79acf5bbfb2e 100644 (file)
@@ -167,12 +167,28 @@ pub fn compile_pkg(package: &Package, options: &CompileOptions)
     let to_build = packages.iter().find(|p| p.package_id() == pkgid).unwrap();
     let targets = try!(generate_targets(to_build, mode, filter, release));
 
+    let target_with_args = match target_rustc_args {
+        &Some(args) => {
+            if targets.len() > 1 {
+                return Err(human("extra arguments to `rustc` can only be \
+                                  invoked for one target"))
+            }
+            let (target, profile) = targets[0];
+            let mut profile = profile.clone();
+            profile.rustc_args = Some(args.to_vec());
+            Some((target, profile))
+        },
+        &None => None,
+    };
+
+    let targets = target_with_args.as_ref().map(|&(t, ref p)| vec!((t, p)))
+                                           .unwrap_or(targets);
+
     let ret = {
         let _p = profile::start("compiling");
         let mut build_config = try!(scrape_build_config(config, jobs, target));
         build_config.exec_engine = exec_engine.clone();
         build_config.release = release;
-        build_config.target_rustc_args = target_rustc_args.map(|a| a.to_vec());
         if let CompileMode::Doc { deps } = mode {
             build_config.doc_all = deps;
         }
index a4a5bb1fc0879f5f94e4db1c78c5d6db0fb3dc61..839ec31f7231fb4f0a1321f293f3309ac10e248e 100644 (file)
@@ -43,7 +43,6 @@ pub struct BuildConfig {
     pub exec_engine: Option<Arc<Box<ExecEngine>>>,
     pub release: bool,
     pub doc_all: bool,
-    pub target_rustc_args: Option<Vec<String>>,
 }
 
 #[derive(Clone, Default)]
@@ -624,7 +623,6 @@ fn build_base_args(cx: &Context,
         opt_level, lto, codegen_units, ref rustc_args, debuginfo, debug_assertions,
         rpath, test, doc: _doc,
     } = *profile;
-    let _ = rustc_args;
 
     // Move to cwd so the root_path() passed below is actually correct
     cmd.cwd(cx.config.cwd());
@@ -666,7 +664,7 @@ fn build_base_args(cx: &Context,
         cmd.arg("-g");
     }
 
-    if let Some(ref args) = cx.build_config.target_rustc_args {
+    if let &Some(ref args) = rustc_args {
         cmd.args(args);
     }
 
index 87fd961a5845b4fb7eb960b168e712ec0e0c7dc7..f370986a680bc2ba67fb98e7119043d6c6a5e084 100644 (file)
@@ -91,3 +91,25 @@ test!(build_main_and_allow_unstable_options {
                 .with_stdout(verbose_output_for_target_with_args(false, &p,
                                                                  "-Z unstable-options")));
 });
+
+test!(fails_when_trying_to_build_main_and_lib_with_args {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+
+            name = "foo"
+            version = "0.0.1"
+            authors = ["wycats@example.com"]
+        "#)
+        .file("src/main.rs", r#"
+            fn main() {}
+        "#)
+        .file("src/lib.rs", r#" "#);
+
+
+    assert_that(p.cargo_process("rustc").arg("-v")
+                .arg("--").arg("-Z").arg("unstable-options"),
+                execs()
+                .with_status(101)
+                .with_stderr("extra arguments to `rustc` can only be invoked for one target"));
+});